(function ($) {
    phoneTime = 60;
    codeTime = 0;
    flag = false;

    submit1 = function (code, phone) {
        if (codeTime < 300) {
            $.ajax({
                url: Store.context + "/user/resetPassword1.do",
                type: "post",
                data: {
                    code: code,
                    phone: phone
                },
                success: function (data) {
                    if(data.success){
                        var form1 = document.createElement("form");
                        form1.action = "/client/user/gotoResetPassword.do";
                        form1.method = "post";
                        var input1 = document.createElement("input");
                        input1.name = "key";
                        input1.value = data.msg;
                        form1.appendChild(input1);
                        var input2 = document.createElement("input");
                        input2.name = "page";
                        input2.value = 2;
                        form1.appendChild(input2);
                        document.body.appendChild(form1);
                        form1.submit();
                    }else{
                        $(".codecheck").html("手机验证码有误")
                    }
                }
            })
        }else{
            $(".codecheck").html("您的手机验证码已过期")
        }
    }


    submit2 = function () {
        if(checkPassword()){
            var p  = $("input[name='password']");
            $.ajax({
                url:"/client/user/resetPassword2.do",
                type:"post",
                data:{
                    key:$("#key").val(),
                    password:hex_md5(p.val())
                },
                success:function (data) {
                    if(data.success){
                        var form3 = document.createElement("form");
                        form3.action = "/client/user/gotoResetPassword.do";
                        form3.method = "post";
                        var input1 = document.createElement("input");
                        input1.type = "hidden";
                        input1.name = "page";
                        input1.value = 3;
                        form3.appendChild(input1);
                        document.body.appendChild(form3);
                        form3.submit();
                    }else{
                        if(data.other == -1){
                            window.setTimeout(function () {
                                var form2 = document.createElement("form");
                                form2.action = "/client/user/gotoResetPassword.do";
                                form2.method = "post";
                                var input1 = document.createElement("input");
                                input1.type = "hidden";
                                input1.name = "page";
                                input1.value = 1;
                                form1.appendChild(input1);
                                document.body.appendChild(form2);
                                form2.submit();
                            },2000);
                            prompt(data.msg,1500);
                        }else if(data.other == 1){
                            prompt(data.msg,1500);
                            window.setTimeout(function () {
                                window.location.reload();
                            },2000)
                        }

                    }
                }
            })
        }
    }



    checkPassword = function () {
        var p1 = $("input[name='password']").val();
        var p2 = $("input[name='passwordconfirm']").val();
        if(p1 == p2 ) {
            return true;
        }else{
            alert("密码不一致");
            return false;
        }
    }


    //发送短信验证码
    getPhoneCode = function (phone) {
        $.ajax({
            url: Store.context + "/user/sendPhoneCode.do",
            type: 'post',
            data: {
                phone: phone,
            },
            success: function (data) {
                time();
                codetime();
            }
        })
        flag = true;
    }


    //过期计算器
    codetime = function () {
        setInterval(function () {
            codeTime++;
        }, 1000)
    }
    //时间计算器
    time = function () {
        var _this = this;
        var t = $("#phonetime");
        if (phoneTime == 0) {
            codeTime = 0;
            phoneTime = 60;
            t.removeAttr("disabled");
            t.val("获取验证码");
        } else {
            t.attr('disabled', true);
            t.val(phoneTime + "秒后可重新发送");
            phoneTime--;
            setTimeout(function () {
                time();
            }, 1000);
        }
    }

    function prompt(text, _time) {
        var windowWidth = $(window).width();
        var tipsDiv = '<div class="tipsClass">' + text + '</div>';
        $('body').append(tipsDiv);
        $('div.tipsClass').css({
            'top': 130 + 'px',
            'left': ( windowWidth / 2 ) - 350 / 2 + 'px',
            'position': 'fixed',
            'padding': '10px 16px',
            'background': '#7FFFD4',
            'font-size': 18 + 'px',
            'margin': '0 auto',
            'text-align': 'center',
            'width': '350px',
            'height': 'auto',
            'color': 'white',
            'opacity': '0.8'
        }).show();
        setTimeout(function () {
            $('div.tipsClass').fadeOut();
        }, _time);
    }

}(jQuery))